package the.workshop.bu.dto.req;

import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import the.broccoli.auth.UserContext;
import the.workshop.bu.dto.CreditDTO;
import the.workshop.bu.entity.CreditProduct;

@Data
public class CreditAddReq {

    /**
     * 客户ID
     */
    private Integer customerId;

    /**
     * 业务员ID
     */
    private String salesId;

    /**
     * 备注
     */
    private String remark;

    /**
     * 产品
     */
    @ApiModelProperty("信审产品，格式[{productId:0, amount:\"0\"}]")
    private String products;

    /**
     * 申贷状态
     */
    private String status;

    public CreditDTO toCreditDTO() {
        CreditDTO dto = new CreditDTO();
        BeanUtils.copyProperties(this, dto);
        dto.setCreatedId(UserContext.userId());
        dto.setCreatedName(UserContext.userName());

        // 业务员为当前用户
        dto.setSalesId(UserContext.userId());

        // 产品列表
        if (StringUtils.hasText(products)) {
            dto.setProducts(JSON.parseArray(products, CreditProduct.class));
        }

        return dto;
    }
}
